package com.ruoyi.safe.controller.web;

import cn.hutool.core.util.ObjectUtil;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.safe.domain.BizExtensionRequests;
import com.ruoyi.safe.domain.BizRectifyReview;
import com.ruoyi.safe.domain.dto.BizRectifyReviewDTO;
import com.ruoyi.safe.service.IBizExtensionRequestsService;
import com.ruoyi.safe.service.IBizRectifyReviewService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 复查--整改Controller
 *
 * @author ruoyi
 * @date 2023-11-22
 */
@RestController
@RequestMapping("/review/review")
public class BizRectifyReviewController extends BaseController {
    @Autowired
    private IBizRectifyReviewService bizRectifyReviewService;
    @Autowired
    private IBizExtensionRequestsService bizExtensionRequestsService;

    /**
     * 查询复查--整改列表
     */
    @PreAuthorize("@ss.hasPermi('review:review:list')")
    @GetMapping("/list")
    public TableDataInfo list(BizRectifyReview bizRectifyReview) {
        startPage();
        List<BizRectifyReview> list = bizRectifyReviewService.selectBizRectifyReviewList(bizRectifyReview);
        return getDataTable(list);
    }

    /**
     * 导出复查--整改列表
     */
    @PreAuthorize("@ss.hasPermi('review:review:export')")
    @Log(title = "复查--整改", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BizRectifyReview bizRectifyReview) {
        List<BizRectifyReview> list = bizRectifyReviewService.selectBizRectifyReviewList(bizRectifyReview);
        ExcelUtil<BizRectifyReview> util = new ExcelUtil<BizRectifyReview>(BizRectifyReview.class);
        util.exportExcel(response, list, "复查--整改数据");
    }

    /**
     * 获取复查--整改详细信息
     */
    @PreAuthorize("@ss.hasPermi('review:review:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(bizRectifyReviewService.selectBizRectifyReviewById(id));
    }

    /**
     * 新增复查--整改
     */
    @PreAuthorize("@ss.hasPermi('review:review:add')")
    @Log(title = "复查--整改", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BizRectifyReviewDTO bizRectifyReview) {
        if (ObjectUtil.isNull(bizRectifyReview.getProjectId())) {
            throw new RuntimeException("项目id不能为空");
        }
        if (ObjectUtil.isEmpty(bizRectifyReview.getOperatorExplain()) && bizRectifyReview.getOperatorType().equals("1")) {
            throw new RuntimeException("复查说明不能为空");
        }
        if (ObjectUtil.isEmpty(bizRectifyReview.getOperatorExplain()) && bizRectifyReview.getOperatorType().equals("2")) {
            throw new RuntimeException("整改说明不能为空");
        }

        return toAjax(bizRectifyReviewService.addRectifyReview(bizRectifyReview));
    }

    /**
     * 修改复查--整改
     */
    @PreAuthorize("@ss.hasPermi('review:review:edit')")
    @Log(title = "复查--整改", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BizRectifyReview bizRectifyReview) {
        return toAjax(bizRectifyReviewService.updateBizRectifyReview(bizRectifyReview));
    }

    /**
     * 删除复查--整改
     */
    @PreAuthorize("@ss.hasPermi('review:review:remove')")
    @Log(title = "复查--整改", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(bizRectifyReviewService.deleteBizRectifyReviewByIds(ids));
    }

    /**
     * 申请延期
     */
    @PutMapping("/apply")
    public AjaxResult apply(@RequestBody BizExtensionRequests bizExtensionRequests) {
        return toAjax(bizExtensionRequestsService.insertBizExtensionRequests(bizExtensionRequests));
    }

}
